<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 循环饮用:a = { 类型b} b = {类型c} c = {类型a}
        // 使用json.stringify()的时候会报错

        function isCycleObject(obj, parent) {
            const parentArr = parent || []
            for (let i in obj) {
                if (typeof obj[i] === 'object') {
                    let flag = false;
                    parentArr.forEach((pobj) => {
                        if (pobj === obj[i]) {
                            flag = true;
                        }
                    })
                    if (flag === true) return true;

                    flag = isCycleObject(obj[i], [...parentArr, obj[i]])
                    if (flag === true) return true;
                }
            }
            return false // 没有循环引用
        } 
        const a = 1;
        const b = { a };
        const c = { b };
        const o = { d: { a: 3 }, c }
        o.c.b.aa = a;

        console.log(isCycleObject(o))
    </script>
</body>

</html>